package top.saodisheng.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.saodisheng.pojo.Package;
import top.saodisheng.service.PackageService;
import top.saodisheng.util.Page4Navigator;

/**
 * Description:
 *
 * @author 扫地生_saodisheng
 */
@RestController
public class PackageController {
    @Autowired
    PackageService packageService;

    @GetMapping("/packages")
    public Page4Navigator<Package> list(@RequestParam(value = "start", defaultValue = "0") int start,
                                        @RequestParam(value = "size", defaultValue = "5") int size) {
        start = start < 0 ? 0 : start;
        Page4Navigator<Package> page = packageService.list(start, size, 5);
        return page;
    }

    // 获取一个包裹
    @GetMapping("/packages/{id}")
    public Package get(@PathVariable("id") int id) {
        return packageService.get(id);
    }

    // 删除一个包裹
    @DeleteMapping("/packages/{id}")
    public String delete(@PathVariable("id") int id) {
        packageService.delete(id);
        return null;
    }

    // 添加一个包裹
    @PostMapping("/packages")
    public Object add(@RequestBody Package bean) {
        packageService.add(bean);
        return bean;
    }

    // 更新包裹
    @PutMapping("/packages")
    public Object update(@RequestBody Package bean) {
        packageService.update(bean);
        return bean;
    }
}
